#include "page.h"
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "scan.h"
#include "button.h"
#include "led_strip.h"
#include "driver/rmt.h"
#include "mainPage.h"

static led_strip_t *strip;

void led_bar_update_num(led_bar_t bar);
static const char *TAG = "led";
void show_led_page()
{
    lv_group_remove_all_objs(group);
    lv_group_add_obj(group, bar_r.bar);
    lv_group_add_obj(group, bar_g.bar);
    lv_group_add_obj(group, bar_b.bar);
    lv_scr_load(ledPage.page);
    push_page(&ledPage);
}

esp_err_t example_rmt_init(uint8_t gpio_num, int led_number, uint8_t rmt_channel)
{
    ESP_LOGI(TAG, "Initializing WS2812");
    rmt_config_t config = RMT_DEFAULT_CONFIG_TX(gpio_num, rmt_channel);

    /*!< set counter clock to 40MHz */
    config.clk_div = 2;

    ESP_ERROR_CHECK(rmt_config(&config));
    ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));

    led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(led_number, (led_strip_dev_t)config.channel);
    strip = led_strip_new_rmt_ws2812(&strip_config);

    if (!strip)
    {
        ESP_LOGE(TAG, "install WS2812 driver failed");
        return ESP_FAIL;
    }

    /*!< Clear LED strip (turn off all LEDs) */
    ESP_ERROR_CHECK(strip->clear(strip, 100));
    /*!< Show simple rainbow chasing pattern */

    return ESP_OK;
}
// bar回调
static void event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_LEFT)
        {
            value -= 5;
        }
        else if (key == LV_KEY_RIGHT)
        {
            value += 5;
        }
        lv_bar_set_value(obj, value, 0);
        if (obj == bar_r.bar)
        {
            led_bar_update_num(&bar_r);
        }
        else if (obj == bar_g.bar)
        {
            led_bar_update_num(&bar_g);
        }
        else if (obj == bar_b.bar)
        {
            led_bar_update_num(&bar_b);
        }
        ESP_ERROR_CHECK(strip->set_pixel(strip, 0, lv_bar_get_value(bar_r.bar), lv_bar_get_value(bar_g.bar), lv_bar_get_value(bar_b.bar)));
        ESP_ERROR_CHECK(strip->refresh(strip, 0));
    }
}

/******************************led page***********************************/
void led_bar_update_num(led_bar_t bar)
{
    char buf[8];
    lv_snprintf(buf, sizeof(buf), "%d", lv_bar_get_value(bar->bar));
    lv_label_set_text(bar->label, buf);
    lv_obj_align_to(bar->label, bar->bar, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
}
void led_bar_init(led_bar_t bar, int y, lv_color_t start, lv_color_t end)
{
    lv_style_init(&bar->style_bg);
    lv_style_set_bg_opa(&bar->style_bg, LV_OPA_TRANSP);//全覆盖


    lv_style_init(&bar->style_indic);
    lv_style_set_bg_opa(&bar->style_indic, LV_OPA_COVER);
    lv_style_set_bg_color(&bar->style_indic, start);
    lv_style_set_bg_grad_color(&bar->style_indic, end);
    lv_style_set_bg_grad_dir(&bar->style_indic, LV_GRAD_DIR_HOR);

    bar->bar = lv_bar_create(ledPage.page);
    lv_obj_add_style(bar->bar, &bar->style_indic, LV_PART_INDICATOR);
    lv_obj_add_style(bar->bar, &bar->style_bg, 0);
    lv_obj_set_size(bar->bar, 200, 20);
    lv_obj_align(bar->bar, LV_ALIGN_LEFT_MID, 60, y);
    lv_bar_set_range(bar->bar, 0, 255);
    lv_bar_set_value(bar->bar, 255, 0);

    bar->led = lv_led_create(ledPage.page);
    lv_obj_align(bar->led, LV_ALIGN_LEFT_MID, 20, y);
    lv_led_set_color(bar->led, end);
    lv_led_set_brightness(bar->led, 1);
    lv_led_on(bar->led);

    bar->label = lv_label_create(ledPage.page);
    led_bar_update_num(bar);

}
void set_led_num(unsigned char r, unsigned char g, unsigned char b)
{
    lv_bar_set_value(bar_r.bar, r, 0);
    lv_bar_set_value(bar_g.bar, g, 0);
    lv_bar_set_value(bar_b.bar, b, 0);
    led_bar_update_num(&bar_r);
    led_bar_update_num(&bar_g);
    led_bar_update_num(&bar_b);
    ESP_ERROR_CHECK(strip->set_pixel(strip, 0, lv_bar_get_value(bar_r.bar), lv_bar_get_value(bar_g.bar), lv_bar_get_value(bar_b.bar)));
    ESP_ERROR_CHECK(strip->refresh(strip, 0));
}

#define CONFIG_EXAMPLE_RMT_TX_GPIO 45
#define CONFIG_EXAMPLE_STRIP_LED_NUMBER 1
void page_led_init()
{
    ESP_ERROR_CHECK(example_rmt_init(CONFIG_EXAMPLE_RMT_TX_GPIO, CONFIG_EXAMPLE_STRIP_LED_NUMBER, RMT_CHANNEL_0));
    ledPage.page = lv_obj_create(0);

    led_bar_init(&bar_r, -80,  lv_color_hex(0), lv_palette_main(LV_PALETTE_RED));
    led_bar_init(&bar_g, 0,  lv_color_hex(0), lv_palette_main(LV_PALETTE_GREEN));
    led_bar_init(&bar_b, 80,  lv_color_hex(0), lv_palette_main(LV_PALETTE_BLUE));
    lv_obj_add_event_cb(bar_r.bar, event_handler, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(bar_g.bar, event_handler, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(bar_b.bar, event_handler, LV_EVENT_KEY, NULL);

    ESP_ERROR_CHECK(strip->set_pixel(strip, 0, 255, 255, 255));
    ESP_ERROR_CHECK(strip->refresh(strip, 0));


    ledPage.show = show_led_page;
}



